    ;   p2_2.s
    ;
    ;   Toggling LEDs in Assembly
    ;   This program toggles all three LEDs 
    ;   for 0.5 second ON and 0.5 second OFF.
    ;
        THUMB

        AREA    |.text|, CODE, READONLY, ALIGN=2
        ALIGN          
        EXPORT  __main

__main
        ; enable clock to GPIOF at clock gating register
        LDR     R0, =0x400FE608	;RCGC register address
        LDR     R1, [R0]
        ORR     R1, #0x20
        STR     R1, [R0]

        ; set PORTF pin3-1 as output pins
        LDR     R0, =0x40025400	
                ;DIR register address
        MOV     R1, #0x0E
        STR     R1, [R0]

        ; set PORTF pin3-1 as digital pins
        LDR     R0, =0x4002551C	
                ; Digital Enable register address
        MOV     R1, #0x0E
        STR     R1, [R0]

loop
        ; write PORTF to turn on all LEDs
        LDR     R0, =0x400253FC	
                ;PORTF address
        MOV     R1, #0x0E
        STR     R1, [R0]
        ; delay a little while
        LDR     R0, =500
        BL      delayMs	; call delayMs
        
        ; write PORTF to turn off all LEDs
        LDR     R0, =0x400253FC
        MOV     R1, #0
        STR     R1, [R0]
        ; delay a little while
        LDR     R0, =500
        BL      delayMs
        
        ; repeat the loop
        B       loop

   ; This subroutine performs a delay of n ms. 
   ; n is the value in R0
delayMs 
        MOVS    R3, R0
        BNE     L1   ; if n = 0, return
        BX      LR   ; return
L1      LDR     R4, =5336       
        ; do inner loop 5336 times (for 16 MHz CPU clock)
L2      SUBS    R4, R4, #1      ; inner loop
        BNE     L2
        SUBS    R3, R3, #1      ; do outer loop n times
        BNE     L1
        BX      LR
        
    ; This subroutine grants access to 
    ; floating point coprocessor.
    ; It is called by the startup code.
        EXPORT  SystemInit
SystemInit 
        LDR     R0, =0xE000ED88           
                ; Enable CP10,CP11
        LDR     R1,[R0]
        ORR     R1,R1,#(0xF << 20)
        STR     R1,[R0]
        BX      LR

    ; This variable is required for the 
    ; startup code though not used.
        EXPORT  __use_two_region_memory
__use_two_region_memory EQU 1

        ALIGN
        END